package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.FirmMapper;
import com.code.dao.PhoneMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Firm;
import com.code.pojo.Phone;
import com.code.service.PhoneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    PhoneMapper phoneMapper;
    @Autowired
    FirmMapper firmMapper;


    @Override
    public PageResult<Phone> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Phone> phoneList = phoneMapper.findLike(queryPageBean);
        PageInfo<Phone> pageInfo = new PageInfo<>(phoneList);
        PageResult<Phone> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Phone> findPage(Phone phone) {
//     PageHelper.startPage(phone.getPage(),phone.getSize());
//        List<Phone> phoneList = phoneMapper.findLike(phone);
//        PageInfo<Phone> pageInfo = new PageInfo<>(phoneList);
//        PageResult<Phone> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Phone findById(Integer id) {
        return phoneMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Phone phone) {
        phoneMapper.insert(phone);
    }

    @Override
    public void update(Phone phone) {
        phoneMapper.updateByPrimaryKeySelective(phone);
    }

    @Override
    public void delete(Integer id) {
        phoneMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Firm> findFirm() {
        return firmMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return phoneMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return phoneMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Phone> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Phone> phoneList = phoneMapper.findPageNo(queryPageBean);
        PageInfo<Phone> pageInfo = new PageInfo<>(phoneList);
        PageResult<Phone> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Phone phone = phoneMapper.selectByPrimaryKey(id);
        phone.setState(1);
        phoneMapper.updateByPrimaryKeySelective(phone);
    }
}
